package com.jec.module.sysconfig.service;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jec.module.sysconfig.service.configor.BaseConfigor;
import com.jec.module.sysconfig.service.configor.BroadcastConfigor;
import com.jec.module.sysconfig.service.configor.ClockConfigor;
import com.jec.module.sysconfig.service.configor.DigitTrunkConfigor;
import com.jec.module.sysconfig.service.configor.MeetingConfigor;
import com.jec.module.sysconfig.service.configor.NumberEntryConfigor;
import com.jec.module.sysconfig.service.configor.PhoneStationConfigor;
import com.jec.module.sysconfig.service.configor.RelayBusinessConfigor;
import com.jec.module.sysconfig.service.configor.TerminalBusinessConfigor;
import com.jec.module.sysconfig.service.configor.TerminalKeyConfigor;
import com.jec.module.sysconfig.service.configor.TonglingConfigor;
import com.jec.utils.Response;

import lombok.Getter;

@Component
public class Configors {
	
    @Autowired
    @Getter
    private BroadcastConfigor broadcastConfigor;
    
    @Autowired
    @Getter
    private ClockConfigor clockConfigor;
    
    @Autowired
    @Getter
    private DigitTrunkConfigor digitTrunkConfigor;
    
    @Autowired
    @Getter
    private MeetingConfigor meetingConfigor;
    
    @Autowired
    @Getter
    private NumberEntryConfigor numberEntryConfigor;
    
    @Autowired
    @Getter
    private PhoneStationConfigor phoneStationConfigor;
    
    @Autowired
    @Getter
    private TerminalBusinessConfigor terminalBusinessConfigor;
    
    @Autowired
    @Getter
    private TerminalKeyConfigor terminalKeyConfigor;
    
    @Autowired
    @Getter
    private TonglingConfigor tonglingConfigor;
    
    @Autowired
    @Getter
    private RelayBusinessConfigor relayBusinessConfigor;
    
    private List<BaseConfigor<?>> all = Lists.newArrayList();
    
    private Map<String, BaseConfigor<?>> configors = Maps.newHashMap();
    
    @PostConstruct
    public void init(){

    	all.add(clockConfigor);
    	all.add(meetingConfigor);
    	all.add(tonglingConfigor);
    	all.add(digitTrunkConfigor);
    	all.add(numberEntryConfigor);
    	all.add(broadcastConfigor);
    	all.add(phoneStationConfigor);
    	all.add(terminalBusinessConfigor);
    	all.add(terminalKeyConfigor);
    	all.add(relayBusinessConfigor);
    	
        //configors.put("card", (BaseConfigor<?>)context.getBean("cardDownloadService"));
        configors.put("clock", clockConfigor);
        configors.put("meeting", meetingConfigor);
        configors.put("tongling", tonglingConfigor);
        configors.put("digittrunk", digitTrunkConfigor);
        configors.put("userdata", numberEntryConfigor);
        configors.put("broadcast", broadcastConfigor);
        configors.put("phonestation", phoneStationConfigor);
        configors.put("relay", relayBusinessConfigor);
        //configors.put("vdr", (BaseConfigor<?>)context.getBean("vdrDownloadService"));
        //configors.put("terminalbusiness", (BaseConfigor<?>)context.getBean("terminalBusinessDownloadService"));
        //configors.put("terminalkey", (BaseConfigor<?>)context.getBean("terminalKeyDownloadService"));
    }
    
    public Response index(String type, int page, int pageSize) {
    	
        Response response =  Response.Builder();
        
        BaseConfigor<?> clazz = configors.get(type);
        if(clazz == null)
            return response.status(Response.STATUS_PARAM_ERROR);
        else
            return response.data(clazz.load(page, pageSize));
    	
    }
    
    public Response remove(String type, int netunit, int id) {
    	
        BaseConfigor<?> clazz = configors.get(type);
        if(clazz == null)
            return Response.Builder(false);
        else
            return Response.Builder(clazz.delete(id, netunit));
    }
    
    public boolean hasConfigOnNetunit(int netunit) {
    	
    	for(BaseConfigor<?> c : all) {
    		if(c.hasConfigOnNetunit(netunit))
    			return true;
    	}
    	return false;
    }

}
